{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "aaba4dfc-3203-4cdf-be64-4f631557db46",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-04T06:42:03.203344Z",
     "iopub.status.busy": "2022-01-04T06:42:03.202957Z",
     "iopub.status.idle": "2022-01-04T06:42:05.799047Z",
     "shell.execute_reply": "2022-01-04T06:42:05.798384Z",
     "shell.execute_reply.started": "2022-01-04T06:42:03.203278Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import random\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from math import exp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e8fc1805-215d-4d48-b4f7-8cb4371c472f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-04T07:00:40.174984Z",
     "iopub.status.busy": "2022-01-04T07:00:40.174662Z",
     "iopub.status.idle": "2022-01-04T07:00:40.181657Z",
     "shell.execute_reply": "2022-01-04T07:00:40.180971Z",
     "shell.execute_reply.started": "2022-01-04T07:00:40.174956Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "### 参数\n",
    "#Miner数量 [50,120]\n",
    "#ESP 可提供资源 [x_min,x_max]\n",
    "#ESP 单位资源定价 [0,p_max]\n",
    "#Mine winner的固定奖励 R #现行Bitcoin每个区块中新发行的比特币目前为 6.25 个\n",
    "#Mine winner找到的区块 需要打包的大小 t 服从 N(mu,theta)\n",
    "#Mine winner打包区块收益因子 r 1e-5BTC/KB\n",
    "#区块传播时间因子 z\n",
    "#假设 Miner时间服从 poisson 分布 参数 lamda=个数/时间; λ > 0是分布的参数，即每单位时间发生该事件的次数\n",
    "N = 50 \n",
    "x_min = 1e-2\n",
    "x_max = 1e2\n",
    "p_max = 1e2\n",
    "R = 6.25\n",
    "mu = 200\n",
    "theta = 5\n",
    "r = 20\n",
    "z = 5e-3\n",
    "lamda = 1/600"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "45fd2c41-144b-4250-8bb8-b88de35a7c55",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-04T06:42:05.800942Z",
     "iopub.status.busy": "2022-01-04T06:42:05.800473Z",
     "iopub.status.idle": "2022-01-04T06:42:05.805429Z",
     "shell.execute_reply": "2022-01-04T06:42:05.804711Z",
     "shell.execute_reply.started": "2022-01-04T06:42:05.800905Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "np.set_printoptions(suppress=True)   \n",
    "np.set_printoptions(threshold=np.inf)\n",
    "np.random.seed(4977)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "120b3989-7504-436d-b964-106aadf6ffc1",
   "metadata": {},
   "source": [
    "poisson分布    \n",
    "泊松分布就是描述某段时间内，事件具体的发生概率。    \n",
    "$P(N(t)=n)=\\frac{(\\lambda t)^{n} e^{-\\lambda t}}{n !}$ "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fd91d3d-3cf3-49f7-8b01-a9dac3f7d330",
   "metadata": {},
   "source": [
    "引用：\n",
    "```\n",
    "Grunspan C, Pérez-Marco R. The mathematics of Bitcoin[J]. EMS Newsletter, 2020 (115): 31-37.\n",
    "```\n",
    "哈希函数的伪随机特性表明，挖掘是一个马尔可夫过程，即无记忆的。 那么从这个性质证明 T 服从指数分布     \n",
    "指数分布是事件的时间间隔的概率。指数分布的公式可以从泊松分布推断出来。如果下一个block间隔时间 t ，就等同于 t 之内没有任何block被成功挖掘。      \n",
    "$P(X>t)=P(N(t)=0)=\\frac{(\\lambda t)^{0} e^{-\\lambda t}}{0 !}=e^{-\\lambda t}$       \n",
    "反过来，block被成功挖掘在时间 t 之内发生的概率，就是1减去上面的值。    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0447e166-68cb-4111-9b97-3d88d96e2c5b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-04T06:42:08.215666Z",
     "iopub.status.busy": "2022-01-04T06:42:08.215117Z",
     "iopub.status.idle": "2022-01-04T06:42:08.219334Z",
     "shell.execute_reply": "2022-01-04T06:42:08.218718Z",
     "shell.execute_reply.started": "2022-01-04T06:42:08.215626Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "def expo(lam, t):\n",
    "    return exp(-lam * t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5e3af4ee-f3b7-4abe-903c-7d25b3f48e8f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-04T07:00:44.162567Z",
     "iopub.status.busy": "2022-01-04T07:00:44.161730Z",
     "iopub.status.idle": "2022-01-04T07:00:44.168350Z",
     "shell.execute_reply": "2022-01-04T07:00:44.167748Z",
     "shell.execute_reply.started": "2022-01-04T07:00:44.162496Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[192.36366736] 0.9983982536059187\n"
     ]
    }
   ],
   "source": [
    "t = np.random.normal(mu, theta, 1)\n",
    "prob = expo(lamda, t*z)\n",
    "print(t, prob)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "336727d7-950b-4fef-8cdf-9e9b3c96b3b7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from gym import envs\n",
    "print(envs.registry.all())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6dc11c9-8cd7-4e66-b74a-b040f2c6d960",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "paddle:Python",
   "language": "python",
   "name": "conda-env-paddle-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
